
#include <mutex>
#include <condition_variable>
#include <thread>
#include <string>
#include <vector>
#include <atomic>
#include <memory>
using namespace std;

#include "LogFile.hpp"
#include "CountDownLatch.hpp"
#ifndef ASYN_LOGGING_HPP
#define ASYN_LOGGING_HPP

namespace tulun
{
    class AsynLogging
    {
    private:
        void workthreadfunc();

    private:
        const int flushInterval_;
        const std::string basename_;
        const size_t rollSize_;
        std::atomic<bool> running_;

        std::unique_ptr<std::thread> pthread_;
        std::mutex mutex_;
        std::condition_variable cond_;
        std::string currenBuffer_;
        std::vector<std::string> buffers_;
        tulun::LogFile output_;
        CountDownLatch countLatch_;

    public:
        AsynLogging(const std::string basename,
                    const size_t rollSize = 1024 * 1024,
                    int flushInterval = 3);
        ~AsynLogging();

        void append(const std::string &msg);      // C++
        void append(const char *msg, size_t len); // C
        void start();
        void stop();
        void flush();
    };
} // namespace tulun

#endif